import Vue from 'vue'
const checkIdNum = (idNum) => { //日期类型
    let isBlank = false
    let isRight = false;
    let age = 0;
    if (idNum.length == 0) {
        isBlank = true
    } else {
        let pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        isRight = pattern.test(idNum);
    }
    if (isRight) {
        // 身份证号解析
        const codeStr = idNum.toString();
        // 获取年龄
        const myDate = new Date();
        const month = myDate.getMonth() + 1;
        const day = myDate.getDate();
        age = myDate.getFullYear() - codeStr.substring(6, 10) - 1;
        const codeMonth = parseInt(codeStr.substring(10, 12), 10);
        const codeDay = parseInt(codeStr.substring(12, 14), 10);
        if (codeMonth < month || (codeMonth === month && codeDay <= day)) {
            age++;
        }
    }
    return {
        isBlank,
        isRight,
        age,

    };
}


const checkMobile = (value) => {
    const regMobile = /^1[34578]\d{9}$/;
    let isBlank = false
    let isRight = true
    if (value.length == 0) {
        isBlank = true
    }
    else if (!regMobile.test(value)) {
        isRight = false
    }
    return { isBlank, isRight }
};
const checkName = (value) => {
    let isBlank = false
    let isRight = false
    if (value.length == 0) {
        isBlank = true
    }
    else {
        var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
        isRight = pattern.test(value);
    }
    return { isRight, isBlank }
};

const checkPrice = (value) => {
    let isBlank = false
    let isRight = false
    if (value.length == 0) {
        isBlank = true
    }
    else {
        var pattern = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/;
        isRight = pattern.test(value);
    }
    return { isRight, isBlank }
};


export default function (Vue) {
    //添加全局API
    Vue.prototype.$utils = {
        checkIdNum, checkMobile, checkName,checkPrice
    }
}